package klab;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JTextArea;
public class TextAreaOutputStream extends OutputStream {   
    private static final TextAreaOutputStream INSTANCE = new TextAreaOutputStream();   
    private static final PrintStream OUT;   
    private static final PrintStream ERR;
    private static JTextArea outWriter;   
    private static boolean TAINTED = false;   
    static {   
        OUT = System.out;
        ERR = System.err;
        System.setOut(new PrintStream(INSTANCE));  
        System.setErr(new PrintStream(INSTANCE));
    }   
    
    /** Creates a new instance of TextAreaOutputStream. */  
    private TextAreaOutputStream() {}   
    
    /** Gets the output stream. */  
    public static TextAreaOutputStream getInstance(JTextArea textArea) {   
        outWriter = textArea;   
        return INSTANCE;   
    }   
    
    /** Gets the functioning console output.  */
    public static PrintStream getOldSystemOut() {   
        return OUT;   
    }   
    /** Vraca stari System.err */
    public static PrintStream getOldSystemErr() {   
        return ERR;   
    }   
    
    /** Determines if output has occured. */  
    public static boolean isTainted() {   
        return TAINTED;   
    }   
    
    /** Write output to the Text Area. */  
    public void write(int param) {   
        outWriter.setText(outWriter.getText() + (char)param);
        TAINTED = true;   
    }   
} 